package com.saicmotor.paging;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.DataSource;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

import com.saicmotor.paging.Bean.DataBean;

/**
 * @Author xinxinzhang
 * @Date 2022/08/17
 **/
public class PagingViewModel extends ViewModel {
    private int pageSize = 10;
    // PagedList配置
    private PagedList.Config config = new PagedList.Config.Builder()
            .setInitialLoadSizeHint(pageSize)
            .setPageSize(pageSize)// 每页数量
            .setPrefetchDistance(2)
            .setEnablePlaceholders(false)//
            .build();

    // 工厂
    private DataSource.Factory<String, DataBean.ResultBean> factory = new PagingDataSourceFactory();

    // LiveData
    private LiveData<PagedList<DataBean.ResultBean>> mPagedList = new LivePagedListBuilder<>(factory, config)
            .build();

    public LiveData<PagedList<DataBean.ResultBean>> getPagedList() {
        return mPagedList;
    }
}
